package frontend;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.jdesktop.swingx.JXErrorPane;
import org.jdesktop.swingx.error.ErrorInfo;

import util.Constants;

import com.jtattoo.plaf.mint.MintLookAndFeel;

import frontend.base.BasicPanel;
import frontend.bean.ItemBean;
import frontend.container.ClosableTabbedPane;
import frontend.container.mainComponent.FootPanel;
import frontend.container.mainComponent.TopPanel;
import frontend.container.taskPanel.LeftPanel;
import frontend.interf.IAddTabPanel;

/**
 * 
 * this is the main start class
 * 
 * @author zhang.yingchun
 * 
 */
public class MainFrame extends JFrame implements IAddTabPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8863451185255910606L;

	private ClosableTabbedPane tabbedPane = new ClosableTabbedPane();;
	private TopPanel topPanel = new TopPanel("\\icons\\head1.jpg");
	private FootPanel footPanel = new FootPanel("\\icons\\bottom.jpg");
	private LeftPanel leftPanel = new LeftPanel(this);
	private JScrollPane leftScrollPane = null;
	private JSplitPane splitPane = null;

	private void initMainPanel() {

		splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftScrollPane,
				tabbedPane);
		splitPane.setOneTouchExpandable(true);

		// Provide minimum sizes for the two components in the split pane
		Dimension minimumSize = leftPanel.getPreferredSize();

		if (minimumSize.width < 150) {
			minimumSize.width = 150;
			leftPanel.setPreferredSize(minimumSize);
		}
		splitPane.setDividerLocation(minimumSize.width
				+ Constants.LEFT_PANEL_GAP);
		leftScrollPane.setMinimumSize(minimumSize);
	}

	private void initTopPanel() {
		
	}

	private void initLeftPanel() {
		// TODO for test
		List<ItemBean> litemList = new ArrayList<ItemBean>();
		ItemBean bean = new ItemBean();
		bean.setTitle(true);
		bean.setId("1");
		bean.setText("数据展示");
		litemList.add(bean);
		bean = new ItemBean();
		bean.setId("2");
		bean.setText("实时数据展示");
		bean.setToolTip("点击打开实时数据展示画面");
		bean.setClassName("frontend.container.TestPanel");
		litemList.add(bean);
		bean = new ItemBean();
		bean.setId("3");
		bean.setText("实时数据曲线展示");
		bean.setToolTip("点击打开实时数据曲线展示画面");
		bean.setClassName("frontend.container.TestPanel");
		litemList.add(bean);
		bean = new ItemBean();
		bean.setId("4");
		bean.setText("历史数据展示");
		bean.setToolTip("点击打开历史数据展示画面");
		bean.setClassName("frontend.datashow.historyShow.HistoryQueryPanel");
		litemList.add(bean);
		bean = new ItemBean();
		bean.setId("5");
		bean.setText("历史数据曲线展示");
		bean.setToolTip("点击打开历史数据曲线展示画面");
		bean.setClassName("frontend.container.TestPanel");
		litemList.add(bean);
		bean = new ItemBean();
		bean.setId("6");
		bean.setText("参数设置");
		bean.setTitle(true);
		litemList.add(bean);
		bean = new ItemBean();
		bean.setId("7");
		bean.setText("采集间隔时间设置");
		bean.setToolTip("点击打开采集间隔时间设置画面");
		bean.setClassName("frontend.container.TestPanel");
		litemList.add(bean);
		bean = new ItemBean();
		bean.setId("8");
		bean.setText("历史数据曲线展示");
		bean.setToolTip("点击打开历史数据曲线展示画面");
		bean.setClassName("frontend.container.TestPanel");
		litemList.add(bean);
		bean = new ItemBean();
		bean.setId("9");
		bean.setText("测量点设置");
		bean.setToolTip("点击打开测量点设置画面");
		bean.setClassName("frontend.container.TestPanel");
		litemList.add(bean);
		bean = new ItemBean();
		bean.setId("10");
		bean.setText("测量项目设置");
		bean.setToolTip("点击打开测量项目设置画面");
		bean.setClassName("frontend.container.TestPanel");
		litemList.add(bean);
		bean = new ItemBean();
		bean.setId("11");
		bean.setTitle(true);
		bean.setText("系统管理");
		litemList.add(bean);
		bean = new ItemBean();
		bean.setId("12");
		bean.setText("用户信息");
		bean.setToolTip("点击打开用户信息画面");
		bean.setClassName("frontend.container.TestPanel");
		litemList.add(bean);
		leftPanel.setListItem(litemList);
		leftScrollPane = new JScrollPane(leftPanel);
	}

	private void initFootPanel() {
		footPanel.LayoutComponents();
	}

	public MainFrame() {
		JFrame.setDefaultLookAndFeelDecorated(true);

        try {
			UIManager.setLookAndFeel(new MintLookAndFeel());
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Container contentPane = getContentPane();
		contentPane.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		initTopPanel();
		initFootPanel();
		initLeftPanel();
		initTabPanelDemo1();
		initMainPanel();

		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.weightx = 1;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.anchor = GridBagConstraints.LINE_START;
		contentPane.add(topPanel, gbc);

		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.weighty = 1;
		gbc.anchor = GridBagConstraints.CENTER;
		contentPane.add(splitPane, gbc);
		//
		gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.weighty = 0;
		gbc.anchor = GridBagConstraints.LINE_END;
		contentPane.add(footPanel, gbc);

		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		setSize(new Dimension(1024, 768));
//		setExtendedState(JFrame.MAXIMIZED_BOTH);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MainFrame mainFrame = new MainFrame();
	}

	private void initTabPanelDemo1() {
		tabbedPane = new ClosableTabbedPane();
		JButton btn = new JButton("click");
		// getContentPane().add(tabbedPane);
		tabbedPane.addTab("test", new JLabel(""));
		tabbedPane.addTab("test2", btn);

		ErrorInfo errorInfo = new ErrorInfo(
				"Error",
				"Normally this place contains problem description.\n You see this text because one of the following reasons:\n * Either it is a test\n * Developer have not provided error details\n * This error message was invoked unexpectedly and there are no more details available",
				null, null, null, null, null);

		btn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					int[] i = { 0 };
					System.out.println(i[7]);
				} catch (IndexOutOfBoundsException ex) {
					ex.printStackTrace();
					JXErrorPane.showDialog(tabbedPane, new ErrorInfo(
							"Application Error", "2", null, "3", ex,
							Level.SEVERE, null));
				}
			}
		});
	}

	private void initTabPanelDemo2() {
		tabbedPane = new ClosableTabbedPane() {
			public boolean tabAboutToClose(int tabIndex) {
				String tab = tabbedPane.getTabTitleAt(tabIndex);
				int choice = JOptionPane.showConfirmDialog(null,
						"You are about to close '" + tab
								+ "'\nDo you want to proceed ?",
						"Confirmation Dialog", JOptionPane.INFORMATION_MESSAGE);
				return choice == 0;
				// if returned false tab
				// closing will be canceled
			}
		};
	}

	
	/**
	 * add new tab panel, if find the same title tab, the select it otherwise add a new tab panel with given title.
	 * @param panel
	 */
	public void addTabPanel(BasicPanel panel) {
		for (int i = 0; i < tabbedPane.getTabCount(); i++) {
			String title = tabbedPane.getTabTitleAt(i);
			if(panel.getTitle().trim().equals(title.trim())) {
				tabbedPane.setSelectedIndex(i);
				return;
			}
		}
		tabbedPane.addTab(panel.getTitle(), panel);
		tabbedPane.setSelectedComponent(panel);
	}

}
